﻿Imports CommunityCredit.CommunityCreditService
Imports CommunityCredit
Imports CommunityCredit.Components

Public Class Main

#Region "Private Variables "

    Private _Client As CommunityCreditService = New CommunityCreditService()
    Private _Categories As PointCategoryCollection = New PointCategoryCollection()

#End Region

#Region " Toolstrip Events "

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        LoadCategorieAreas()
    End Sub

#End Region

#Region " Identinfication Control Events "

    Private Sub LoadSettings()
        txtFirstname.Text = My.Settings.Firstname
        txtLastname.Text = My.Settings.Lastname
        txtEmail.Text = My.Settings.Email
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Application.Exit()
    End Sub

    Private Sub txtFirstname_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFirstname.Click
        txtFirstname.SelectAll()
    End Sub

    Private Sub txtLastname_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLastname.Click
        txtLastname.SelectAll()
    End Sub

    Private Sub txtEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmail.Click
        txtEmail.SelectAll()
    End Sub

    Private Sub txtLastname_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLastname.TextChanged
        My.Settings.Lastname = txtLastname.Text
    End Sub

    Private Sub txtFirstname_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFirstname.TextChanged
        My.Settings.Firstname = txtFirstname.Text
    End Sub

    Private Sub txtEmail_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged
        My.Settings.Email = txtEmail.Text
    End Sub

#End Region

#Region " Control Events "

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            LoadSettings()
            LoadCategorieAreas()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub lstAreas_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstAreas.SelectedIndexChanged
        Dim pc As PointCategory = DirectCast(lstAreas.SelectedItem, PointCategory)
        If pc IsNot Nothing Then
            Dim FilteredPointsCategories As New List(Of PointCategory)
            For Each item As PointCategory In _Categories

                If item.Area = pc.Area AndAlso item.IsActive = True Then
                    FilteredPointsCategories.Add(item)
                End If
            Next

            dgCategories.DataSource = FilteredPointsCategories

            dgCategories.Columns("DescriptionDisplay").SortMode = DataGridViewColumnSortMode.Automatic
            dgCategories.Columns("DescriptionDisplay").DisplayIndex = 0
            dgCategories.Columns("Value").SortMode = DataGridViewColumnSortMode.Automatic
            dgCategories.Columns("Value").DisplayIndex = 1
            dgCategories.Columns("Value").AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
            dgCategories.Columns("Area").Visible = False
            dgCategories.Columns("ID").Visible = False
            dgCategories.Columns("Code").Visible = False
            dgCategories.Columns("Description").Visible = False
            dgCategories.Columns("DisplayNamePlural").Visible = False
            dgCategories.Columns("IsActive").Visible = False
            dgCategories.Columns("IsIneta").Visible = False
        End If
    End Sub

    Private Sub dgCategories_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgCategories.SelectionChanged

        For Each oRow In dgCategories.SelectedRows
            lblDescription.Text = oRow.Cells("DescriptionDisplay").Value.ToString
            lblPoints.Text = FormatNumber(oRow.Cells("Value").Value.ToString, 0)
        Next
        ViewerContainer.Panel2Collapsed = False
        grpCreditInformation.Enabled = True
    End Sub

    Private Sub btnAddCredit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCredit.Click
        AddCredit()
    End Sub

#End Region

#Region " Helper Methods "

    Private Sub AddCredit()
        Try
            Dim earner As Earner = New Earner(txtFirstname.Text, txtLastname.Text, txtEmail.Text)
            Dim category As PointCategory = CType(lstAreas.SelectedItem, PointCategory)
            Dim task As Task = New Task(txtTaskDescription.Text, txtTaskUrl.Text, category)

            If (String.IsNullOrEmpty(dtpTaskDate.Text)) Then
                _Client.AddCommunityCredit(earner, task)
            Else
                Dim dateEarned As DateTime = DateTime.Parse(dtpTaskDate.Text)
                _Client.AddCommunityCredit(earner, task, dateEarned)
            End If

            MessageBox.Show("Your credit is added successfully!")

        Catch ex As CommunityCreditException
            MessageBox.Show(ex.Message + ex.StackTrace, "Error")
        End Try
    End Sub

    Private Sub LoadCategorieAreas()
        lstAreas.Items.Clear()

        'Get the categories from Community-credit
        _Categories = _Client.GetPointCategories()

        'Add the first category to the empty list
        For Each Category As PointCategory In _Categories

            If Not AreaIsInList(Category.Area) Then
                lstAreas.Items.Add(Category)
            End If
        Next

    End Sub

    Private Function AreaIsInList(ByVal Area As String) As Boolean
        Dim IsInList As Boolean = False
        If lstAreas.Items.Count > 0 Then
            For Each item As PointCategory In lstAreas.Items
                If Area = item.Area Then
                    IsInList = True
                End If
            Next
        End If
        Return IsInList
    End Function

#End Region

End Class
